package com.globallogic.strelchuk.multi.util;

import com.globallogic.strelchuk.multi.entity.Row;
import com.globallogic.strelchuk.multi.entity.SquareMultiplicationTable;
import com.globallogic.strelchuk.multi.entity.TriangleMultiplicationTable;

/**
 * @author olena.strelchuk
 */

/**
 * Formats Multiplication Tables
 */
public class Formatter {

	private static final String FORMAT_4D = "%4d";
	private static final String NUMBERS_DELIMITER = " |";
	private static final String DELIMITER = "\n------------------------------------------\n";

	public static String formatSquareTable(SquareMultiplicationTable table) {
		String res = drawHeader(table.getTableSize());

		for (int i = 1; i <= table.getTableSize(); i++) {
			res += String.format(FORMAT_4D + NUMBERS_DELIMITER, i);
			for (int j = 1; j <= table.getTableSize(); j++) {
				res += String.format(FORMAT_4D,
						table.getSquareTable()[i - 1][j - 1]);
			}
			res += ("\n");
		}
		return res;
	}

	public static String formatTriangleTable(TriangleMultiplicationTable table) {
		String res = drawHeader(table.getTableSize());

		for (int i = 1; i <= table.getTableSize(); i++) {
			res += String.format(FORMAT_4D + NUMBERS_DELIMITER, i);
			res += formatRow(table.getRowList().get(i - 1));
			res += ("\n");
		}
		return res;
	}

	private static String drawHeader(int size) {
		String res = ("      ");
		for (int i = 1; i <= size; i++) {
			res += String.format(FORMAT_4D, i);
		}
		res += DELIMITER;
		return res;		
	}
	
	private static String formatRow(Row row) {
		String res = "";
		for (int i = 1; i <= row.getRowArray().length; i++) {
			res += String.format(FORMAT_4D, row.getRowArray()[i - 1]);
		}
		return res;
	}

}
